package scanner;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Monitor{
 private String path;//监控路径
 private int ScanTime=0;//扫描时间间隔
 private Map<String,String> FileMap;
 private TransferFile transfer;
 private String[] FileNames=new String[100];
 private int count=0;//记录传送文件的数量
 private int[] flag=new int[100];
 private int n=3;//创建线程的最大数量
 
 public static void main(String args[]){
    Monitor monitor=new Monitor("D:\\cet4",1);
    monitor.checkFile();
 }
 
 public Monitor(String path,int ScanTime){
  this.path=path;
  this.ScanTime=ScanTime;
  this.FileMap=new HashMap<String,String>();
  File file=new File(path);
     ScanFile(file,FileMap);//扫描原有目录下的文件
     ShowFile();//显示文件夹下的文件内容
 }
 
    public void checkFile(){
     File file=new File(path);
     ExecutorService exec = Executors.newFixedThreadPool(n);//构建线程池
     while(true){
      try{
      Thread.sleep(1000);
      Map<String,String> nowFileMap=new HashMap<String,String>();
      ScanFile(file,nowFileMap);
      int f=getAddFileMap(nowFileMap);//获得新增加的文件
      if(f==1){
       for(int i=0;i<count;i++){
            ThreadMonitor threadMo=new ThreadMonitor(FileNames[i]);
            Thread t1 = new Thread(threadMo);
            exec.execute(t1);
         for(int j=i;j<count;j++)//删除处理后文件的名字
         {
          FileNames[j]=FileNames[j+1];
         }
         count--;
       }
      }
      }
      catch(Exception e)
      {
       e.printStackTrace();
      }
     }
     
    }
    public int getAddFileMap(Map<String,String> nowFileMap){
     Iterator<String> iterator=nowFileMap.keySet().iterator();
     while(iterator.hasNext())
     {
      String key=iterator.next();
      if(FileMap.get(key)==null)
      {
       FileNames[count]=key;
       count++;
       FileMap.put(key, nowFileMap.get(key));
       System.out.println("增加："+nowFileMap.get(key)+count);
      }
       
     }
     if(count!=0)
            {
             return 1;
            }
     return 0;
    }
    public void ShowFile()
    {
     System.out.print(path+"文件目录如下：\n");
     Set<String> set=FileMap.keySet();
     Iterator<String> iterator=set.iterator();//迭代器
     while(iterator.hasNext())
     {
      System.out.println(FileMap.get(iterator.next()));
     }
     System.out.println("==============================");
    }
    //扫描文件夹下的所有内容
    public void ScanFile(File file,Map<String,String> oldFileMap)
    {
       String[] FileList=file.list();
//       System.out.print(FileList[0]+"dsd");
       if(FileList!=null)//文件列表不为空
       {
        for(int i=0;i<FileList.length;i++)
        {
         String name=file.getAbsolutePath()+"\\"+FileList[i];
         File tempFile=new File(name);
         if(tempFile.isDirectory())
         {
            oldFileMap.put(name, "文件夹 \t"+name);
            ScanFile(tempFile,oldFileMap);
         }
         else
         {
          oldFileMap.put(name, "文件 \t"+name);
         }
        }
       }
       else
       {
        System.out.print("目前文件夹中还没有文件！");
       }
    }
}
class ThreadMonitor implements Runnable{
 private String name=null;
 private TransferFile transfer;
 public ThreadMonitor(String name)
 {
  this.name=name;
 }
 public void run()
 {
  int flag=2;
  while(true)
  {
   try{
   Thread.sleep(1000);
   transfer=new TransferFile();
      flag=transfer.copyFile(this.name,"d:/abc");
      if(flag==0)//代表本文件传输完毕
      {
       break;
      }
   }
   catch(Exception e)
   {
    e.getStackTrace();
   }
  }
  System.out.print(this.name+":"+Thread.currentThread().getName()+"运行完毕");
 }
}